package com.spring.sunflower.main.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.spring.sunflower.R;
import com.spring.sunflower.bean.VideoPlayBean;
import com.spring.sunflower.util.cache.PreloadManager;
import com.spring.sunflower.video.LoadingView;
import com.spring.sunflower.video.TikTokView;

import java.util.List;


public class TikTokShowAdapter extends RecyclerView.Adapter<TikTokShowAdapter.VideoHolder> {

    private List<VideoPlayBean.DatasBean> videos;
    private Context context;

    public TikTokShowAdapter(Context context, List<VideoPlayBean.DatasBean> videos) {
        this.videos = videos;
        this.context = context;
    }

    private static final String TAG = "TikTokAdapter";

    public void addData(List<VideoPlayBean.DatasBean> data) {
        videos.addAll(data);
        notifyDataSetChanged();
    }

    public void refreshData(List<VideoPlayBean.DatasBean> data) {
        videos.clear();
        videos.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(int pos) {
        notifyItemChanged(pos);
    }

    @Override
    public VideoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder: ");
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_item_video_play_show, parent, false);
        return new VideoHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final VideoHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: " + position);
        VideoPlayBean.DatasBean item = videos.get(position);
        String status = item.getStatus();
        Glide.with(context)
                .load(item.getVideoFirstPhotoUrl())
                .placeholder(R.drawable.ic_bg_video_play)
                .into(holder.thumb);
        Glide.with(context)
                .load(item.getPhoto())
                .placeholder(R.drawable.ic_placeholder)
                .into(holder.rivAvatar);
        if ("4".equals(status)) {
            Glide.with(context)
                    .load(R.drawable.ic_more_video_call)
                    .into(holder.ivDone);
            holder.tvDone.setText("视频聊天");
            holder.tvOnline.setVisibility(View.VISIBLE);
            holder.rivAvatar.setBorderColor(ContextCompat.getColor(context, R.color.colorPrimary));
        } else {
            Glide.with(context)
                    .load(R.drawable.ic_greet)
                    .into(holder.ivDone);
            holder.tvDone.setText("搭个讪");
            holder.tvOnline.setVisibility(View.GONE);
            holder.rivAvatar.setBorderColor(ContextCompat.getColor(context, R.color.white));
        }

        holder.tvLike.setText(item.getLikeTotal());
        holder.tvComment.setText(item.getCommentTotal().equals("0") ? "评论" : item.getCommentTotal());
        holder.tvLook.setText(item.getBrowseTotal());
        holder.tvTitle.setText("@" + item.getNickname());
        if ("1".equals(item.getIsLike())) {
            holder.ivFollowed.setImageResource(R.drawable.icon_video_star);
        } else {
            holder.ivFollowed.setImageResource(R.drawable.icon_video_unstar);
        }
        if ("1".equals(item.getIsFavorite())) {
            holder.ivAddFavorite.setVisibility(View.GONE);
        } else {
            holder.ivAddFavorite.setVisibility(View.VISIBLE);
        }
        holder.ivFollowed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onLike(position);
                }
            }
        });
        holder.ivComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onComment(position);
                }
            }
        });
        holder.ivAddFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onFollowed(position);
                }
            }
        });
        holder.rivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAvatarClick(position);
                }
            }
        });
        holder.llDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onChatClick(position);
                }
            }
        });
        holder.mPosition = position;
        PreloadManager.getInstance(holder.itemView.getContext()).addPreloadTask(item.getVideoPath(), position);
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull VideoHolder holder) {
        super.onViewDetachedFromWindow(holder);
        VideoPlayBean.DatasBean datasBean = videos.get(holder.mPosition);
        PreloadManager.getInstance(holder.itemView.getContext()).removePreloadTask(datasBean.getVideoPath());
//        holder.loadingView.setTimePeriod(100);
    }

    @Override
    public int getItemCount() {
        return videos.size();
    }

    public static class VideoHolder extends RecyclerView.ViewHolder {

        public ImageView thumb;
        public int mPosition;
        public TikTokView mTikTokView;
        public FrameLayout mPlayerContainer;
        private TextView tvTitle;
        public LoadingView loadingView;
        public TextView tvLook, tvComment, tvLike, tvDone, tvOnline;
        public ImageView ivLook, ivComment, ivFollowed, ivAddFavorite, ivDone;
        public RoundedImageView rivAvatar;
        private LinearLayout llDone;

        VideoHolder(View itemView) {
            super(itemView);
            thumb = itemView.findViewById(R.id.iv_thumb);
            mTikTokView = itemView.findViewById(R.id.tiktok_View);
            mPlayerContainer = itemView.findViewById(R.id.flContainer);
            tvTitle = itemView.findViewById(R.id.tv_title);
            loadingView = itemView.findViewById(R.id.loadView);
            tvLook = itemView.findViewById(R.id.tvLook);
            tvComment = itemView.findViewById(R.id.tvComment);
            tvLike = itemView.findViewById(R.id.tvStar);
            ivLook = itemView.findViewById(R.id.ivLook);
            ivComment = itemView.findViewById(R.id.ivComment);
            ivFollowed = itemView.findViewById(R.id.ivStar);
            rivAvatar = itemView.findViewById(R.id.rivAvatar);
            ivAddFavorite = itemView.findViewById(R.id.ivFollow);
            llDone = itemView.findViewById(R.id.llDone);
            ivDone = itemView.findViewById(R.id.ivDone);
            tvDone = itemView.findViewById(R.id.tvDone);
            tvOnline = itemView.findViewById(R.id.tvOnline);
            itemView.setTag(this);
        }
    }

    private OnVideoShowActionListener listener;

    public void setOnVideoShowActionListener(OnVideoShowActionListener l) {
        listener = l;
    }

    public interface OnVideoShowActionListener {
        void onFollowed(int position);

        void onLike(int position);

        void onComment(int position);

        void onAvatarClick(int position);

        void onChatClick(int position);
    }
}